﻿Add-Type -Path "C:\PowerEX\bin\PowerEx.dll" 

$form = New-Object PowerEx.Form1
$startLocation = Get-Location
$form.ListPath1 = $startLocation
$form.ListPath2 = $startLocation
$dirAndFiles = Get-ChildItem
$PARENT = "[...]"
$form.List1.Items.Add($PARENT) | Out-Null
$form.List2.Items.Add($PARENT) | Out-Null
if ($dirAndFiles.Count -gt 0) 
{
    $form.List1.Items.AddRange($dirAndFiles);
    $form.List2.Items.AddRange($dirAndFiles);
}
$form.Text = Get-Location

function SetListPath($path) 
{
    switch($form.ListActive)
    {
        "List1" { $form.ListPath1 = $path; break }
        "List2" { $form.ListPath2 = $path; break }
        default {Write-Host "NotImplement" -ForegroundColor Red }
    }
}

function GetPath()
{
    switch($form.ListActive)
    {
        "List1" { return $form.ListPath1; break }
        "List2" { return $form.ListPath2; break }
        default {Write-Host "NotImplement" -ForegroundColor Red }
    }
}

function GetList()
{
    switch($form.ListActive)
    {
        "List1" { return $form.List1; break }
        "List2" { return $form.List2; break }
        default {Write-Host "NotImplement" -ForegroundColor Red }
    }
}

function ListUpdate($list, $listPath)
{
    $list.Items.Clear()
    $list.Items.Add($PARENT)
    $folders = Get-ChildItem $listPath
    if($folders.Count -gt 0)
    {
        $list.Items.AddRange($folders)
    }    
}

function Remove() 
{
    $list = GetList
    if($list.SelectedItems.Count -gt 0)
    {
        $path = GetPath
        foreach($item in $list.SelectedItems)
        {
            if($item -ne $PARENT)
            {
                Remove-Item "$path\$item"                           
            }
        }
        ListUpdate -list $list -listPath $path               
    }      
}

function CopyItem()
{    
    $list = GetList
    if($list.SelectedItems.Count -gt 0)
    {
        $path = GetPath
        foreach($item in $list.SelectedItems)
        {
            if($item -ne $PARENT)
            {
                 if($form.ListActive -eq "List1")
                 {   
                     Copy-Item "$path\$item" $form.ListPath2 
                     ListUpdate -list ($form.List2) -listPath ($form.ListPath2)                                   
                 }
                 else
                 {
                     Copy-Item "$path\$item" $form.ListPath1
                     ListUpdate -list ($form.List1) -listPath ($form.ListPath1)
                 }                   
            }
        }
    }
}

function MoveItem()
{    
    $list = GetList
    if($list.SelectedItems.Count -gt 0)
    {
        $path = GetPath
        foreach($item in $list.SelectedItems)
        {
            if($item -ne $PARENT)
            {
                 if($form.ListActive -eq "List1")
                 {   
                     Move-Item "$path\$item" $form.ListPath2                                                        
                 }
                 else
                 {
                     Move-Item "$path\$item" $form.ListPath1                     
                 }                   
            }
        }
        ListUpdate -list ($form.List2) -listPath ($form.ListPath2)
        ListUpdate -list ($form.List1) -listPath ($form.ListPath1)
    }
}

function MoveLocation()
{
    $list = GetList
    if($list.SelectedItems.Count -gt 0)
    {
        Set-Location (GetPath)
        $item = $list.SelectedItems[0]
        if($item -eq $PARENT)
        {
            Set-Location "..\"
            SetListPath -path (Get-Location)            
        }
        else
        {
            if (Test-Path ".\$item")
            { 
                Set-Location ".\$item"
                SetListPath -path (Get-Location)
            }
        }
        $list.Items.Clear()
        $list.Items.Add($PARENT)
        $folders = Get-ChildItem (Get-Location)
        if($folders.Count -gt 0)
        {
            $list.Items.AddRange($folders)
        }
        $form.Text = GetPath        
    }    
}

function View() 
{
    $list = GetList
    if ($list.SelectedItems.Count -gt 0) 
    {
        Set-Location (GetPath)
        foreach ($item in $list.SelectedItems)
        {            
            if ($item -ne $PARENT)
            {
                $view = New-Object PowerEx.View
                $utf16 = [System.Text.Encoding]::Unicode
                $utf8 = [System.Text.Encoding]::UTF8
                $resultBytes = [System.Text.Encoding]::Convert($utf8, $utf16, $utf8.GetBytes((Get-Content $item)))
                $view.Content.AppendText($utf16.GetString($resultBytes))
                $view.ShowDialog()
            }
        }
    } 
}

function Run() 
{
    $list = GetList
    if ($list.SelectedItems.Count -gt 0) 
    {
        Set-Location (GetPath)
        foreach ($item in $list.SelectedItems)
        {            
            if ($item -ne $PARENT)
            {
                start $item
            }
        }
    }
}

function NewDir()
{
    Set-Location (GetPath)
    $dialog = New-Object PowerEx.Dialog
    $dialog.ShowDialog()
    if (($dialog.Value -ne "") -and ($dialog.Value -ne $null))
    {
        New-Item ($dialog.Value) -ItemType "directory"
        switch($form.ListActive)
        {
            "List1" { ListUpdate -listPath $form.ListPath1 -list $form.List1; break }
            "List2" { ListUpdate -listPath $form.ListPath2 -list $form.List2; break }
            default {Write-Host "NotImplement" -ForegroundColor Red }
        }
    }    
}

function NewName()
{
    Set-Location (GetPath)
    $list = GetList
    if($list.SelectedItems.Count -gt 0)
    {
        $item = $list.SelectedItems[0]        
        if($item -ne $PARENT)
        {
            $dialog = New-Object PowerEx.Dialog
            $dialog.TextDialog.Text = $item
            $dialog.ShowDialog()

            if(($dialog.Value -ne "") -and ($dialog.Value -ne $item) -and ($dialog.Value -ne $null))
            {
                Rename-Item $item ($dialog.Value)
                switch($form.ListActive)
                {
                    "List1" { ListUpdate -listPath $form.ListPath1 -list $form.List1; break }
                    "List2" { ListUpdate -listPath $form.ListPath2 -list $form.List2; break }
                    default {Write-Host "NotImplement" -ForegroundColor Red }
                }
            }            
        }
    }
}

function KeyDown() {
    
    switch(($_.KeyCode))
    {
        "Return" { MoveLocation; break }
        "Escape"{ $form.Close(); break }
        "F3" { View; break }
        "F4" { Run; break }
        "F5" { CopyItem; break }
        "F6" { MoveItem; break }
        "F7" { NewDir; break }
        "F8" {NewName; break}
        "Delete" { Remove; break }
    }
}

$form.KeyPreview = $true
$form.List1.add_DoubleClick({Invoke-Expression "MoveLocation"})
$form.List2.add_DoubleClick({Invoke-Expression "MoveLocation"})
$form.Add_KeyDown({Invoke-Expression "KeyDown"})
$form.Run.add_Click({Invoke-Expression "Run"})
$form.View.add_Click({ Invoke-Expression "View" })
$form.Copy.add_Click({ Invoke-Expression "CopyItem" })
$form.B_Move.add_Click({ Invoke-Expression "MoveItem" })
$form.MkDir.add_Click({ Invoke-Expression "NewDir" })
$form.Rename.add_Click({ Invoke-Expression "NewName" })
$form.add_FormClosing({ Set-Location (GetPath) })
$form.ShowDialog()
